package cn.tedu.web.stm.Servlet;

import cn.tedu.web.stm.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/stm/insert")
public class UserInsertServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String id = req.getParameter("id");
        if(id!=null && "null".equals(id)){
            String sql = "INSERT INTO `user`(username,`password`,`name`) VALUES(?,MD5(?),?)";
            JdbcUtils.modify(sql,username,password,name);
        }else {
            String sql = "UPDATE `user` SET `username`=?,`password`=MD5(?),`name`=? WHERE id=?";
            JdbcUtils.modify(sql,username,password,name,id);
        }
        resp.sendRedirect("/Stm/user-list.jsp");
    }
}
